
/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.unnamed.b.atv.utils;

import ohos.agp.components.Component;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.utils.PlainArray;
import ohos.utils.PlainBooleanArray;
import ohos.utils.PlainIntArray;
import ohos.utils.PlainLongArray;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 工具类
 */
public class Util {
    /**
     * Turns an array of colors (integer color values) into an ArrayList of colors.
     *
     * @param colors 颜色数组
     * @return result int集
     */
    public static List<Integer> createColors(int[] colors) {
        List<Integer> result = new ArrayList<>();
        for (int i : colors) {
            result.add(i);
        }
        return result;
    }

    /**
     * 获取文字区域宽度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的宽度
     */
    public static int getTextWidth(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int width = bounds.left + bounds.getWidth();
        return width;
    }

    /**
     * 获取文字区域高度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的高度
     */
    public static int getTextHeight(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int height = bounds.bottom + bounds.getHeight();
        return height;
    }

    /**
     * 判断对象是否为空
     *
     * @param obj 对象
     * @return {@code true}: 为空<br>{@code false}: 不为空
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String && obj.toString().length() == 0) {
            return true;
        }
        if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
            return true;
        }
        if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof PlainArray && ((PlainArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainBooleanArray && ((PlainBooleanArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainIntArray && ((PlainIntArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof PlainLongArray && ((PlainLongArray) obj).size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断对象是否非空
     *
     * @param obj 对象
     * @return {@code true}: 非空<br>{@code false}: 空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 获取文字的宽度和高度
     *
     * @param text  绘制的文字
     * @param paint 画笔
     * @return 文字的宽高
     */
    public static int[] getTextWH(String text, Paint paint) {
        Rect bounds = paint.getTextBounds(text);
        int width = bounds.left + bounds.getWidth();
        int height = bounds.bottom + bounds.getHeight();
        return new int[]{width, height};
    }

    /**
     * 获取屏幕宽高
     *
     * @param context 上下文
     * @return int[] 宽高
     */
    public static int[] getScreenWH(Context context) {
        return new int[]{getDisplay(context).getAttributes().width, getDisplay(context).getAttributes().height};
    }

    /**
     * 获取z轴在location中的最佳位置
     *
     * @param context 上下文
     * @return size 大小
     */
    public static float getZForCamera(Context context) {
        return -6 * getDisplay(context).getAttributes().densityDpi;
    }

    /**
     * 获取Display
     *
     * @param context 上下文
     * @return display 屏幕设计
     */
    public static Display getDisplay(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display;
    }
}
